package web.webSocket.hndler;

import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

@Component
public class SystemWebSocketHandler implements WebSocketHandler {
	
	private static UDPServer udp;
	private static Thread listener;
	private static HashMap<String,ArrayList<WebSocketSession>> unicodeSessions;
	private static HashMap<String,ArrayList<String>> sessionIdUnicodes;
	
	public SystemWebSocketHandler(){
		udp = new UDPServer();
		listener = new Thread(udp);
		listener.start();
	}
	
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        if(sessionIdUnicodes ==null) sessionIdUnicodes = new HashMap<String,ArrayList<String>>();
        ArrayList<String> unicodes = sessionIdUnicodes.get(session.getId());
        if(unicodes == null) unicodes = new ArrayList<String>();
        sessionIdUnicodes.put(session.getId(), unicodes);
    }

    @Override
    public void handleMessage(WebSocketSession wss, WebSocketMessage<?> wsm) throws Exception {
    	ArrayList<String> unicodeArray = sessionIdUnicodes.get(wss.getId());
        if(unicodeArray == null) unicodeArray = new ArrayList<String>();
        if(unicodeSessions == null) unicodeSessions = new HashMap<String,ArrayList<WebSocketSession>>();
    	String unicodes = (String)wsm.getPayload();
    	String[] unicodeList = unicodes.split(",");
        for(int i=0,l=unicodeList.length;i<l;i++){
        	if(unicodeList[i].length() == 0) continue;
        	unicodeArray.add(unicodeList[i]);
        	ArrayList<WebSocketSession> sessionList = unicodeSessions.get(unicodeList[i]);
        	if(sessionList == null) sessionList = new ArrayList<WebSocketSession>();
        	sessionList.add(wss);
        	unicodeSessions.put(unicodeList[i], sessionList);
        }
        sessionIdUnicodes.put(wss.getId(), unicodeArray);
    }

    @Override
    public void handleTransportError(WebSocketSession wss, Throwable thrwbl) throws Exception {
        if(wss.isOpen()){
            wss.close();
        }
       System.out.println("Websocket Error");
    }

    @Override
    public void afterConnectionClosed(WebSocketSession wss, CloseStatus cs) throws Exception {
    	if(sessionIdUnicodes == null) return;
    	ArrayList<String> unicodeList = sessionIdUnicodes.get(wss.getId());
    	if(unicodeList == null) return;
    	if(unicodeSessions == null) {
    		sessionIdUnicodes.remove(wss.getId());
    		return;
    	}
    	for(int i=0,l=unicodeList.size();i<l;i++){
    		ArrayList<WebSocketSession> sessionList = unicodeSessions.get(unicodeList.get(i));
    		if(sessionList == null || sessionList.size() == 0) continue;
    		sessionList.remove(wss);
    		unicodeSessions.put(unicodeList.get(i), sessionList);
    	}
    	sessionIdUnicodes.remove(wss.getId());
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
    
    public static void sendByUnicode(String unicode, String text) throws Exception {
    	if(unicodeSessions == null) return;
    	ArrayList<WebSocketSession> sessionList = unicodeSessions.get(unicode);
    	if(sessionList == null) return;
    	for(int i=0,l=sessionList.size();i<l;i++){
    		WebSocketSession session = sessionList.get(i);
    		session.sendMessage(new TextMessage(text));
    	}
    }
}
